package ch.ethz.inf.shop.model;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

import ch.ethz.inf.shop.model.simpleDatabase.Tuple;

public class ShoppingCartLine {

	public ShoppingCartLine(int cartLineId, int amount, Product product, BigDecimal linePrice) {
		this.cartLineId = cartLineId;
		this.amount = amount;
		this.product = product;
		this.linePrice = linePrice;
	}

	public ShoppingCartLine(final ResultSet rs) throws SQLException {
		this.product = new Product(rs);
		this.amount = rs.getInt("amount");
		this.cartLineId = rs.getInt("shopping_cart_line_id");
		this.linePrice = rs.getBigDecimal("line_price");
	}

	public ShoppingCartLine(final Tuple tuple) {
		this.product = new Product(tuple);
		this.amount = tuple.getInt("shopping_cart_line.amount");
		this.cartLineId = tuple.getInt("shopping_cart_line.shopping_cart_line_id");
		this.linePrice = tuple.getBigDecimal("shopping_cart_line.line_price");
	}

	final private int amount;
	final private Product product;
	final private BigDecimal linePrice;
	final private int cartLineId;

	public int getCartLineId() {
		return cartLineId;
	}

	public int getAmount() {
		return amount;
	}

	public BigDecimal getLinePrice() {
		return linePrice;
	}

	public String getName() {
		return product.getName();
	}

	public int getWeight() {
		return product.getWeight();
	}

	public String getCountryOfOrigin() {
		return product.getCountryOfOrigin();
	}

	public Category getCategory() {
		return product.getCategory();
	}

	public Brand getBrand() {
		return product.getBrand();
	}

	public int getProductId() {
		return product.getProductId();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + amount;
		result = prime * result + cartLineId;
		result = prime * result + ((linePrice == null) ? 0 : linePrice.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ShoppingCartLine other = (ShoppingCartLine) obj;
		if (amount != other.amount)
			return false;
		if (cartLineId != other.cartLineId)
			return false;
		if (linePrice == null) {
			if (other.linePrice != null)
				return false;
		} else if (!linePrice.equals(other.linePrice))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		return true;
	}
}
